package com.autonavi.yunda.yunji.core.engine.argresolver;

import com.autonavi.yunda.yunji.core.enums.ParamSourceType;
import com.autonavi.yunda.yunji.core.utils.ValidUtil;
import com.autonavi.yunda.yunji.core.vo.HttpRequest;
import com.autonavi.yunda.yunji.core.vo.param.Param;

import static org.springframework.web.bind.annotation.ValueConstants.DEFAULT_NONE;

/**
 * @author cizhong.wcs
 * @date 2022/6/2 14:15
 */
public class HeaderParamResolver implements ParamResolver {
    @Override
    public boolean supportsParam(Param param) {
        return ParamSourceType.headers.equals(param.source);
    }

    @Override
    public Object resolve(HttpRequest request, Param param) {
        //headers中的key都应是小写
        Object arg = request.getHeaders().get(param.name.toLowerCase());
        if (arg == null && !DEFAULT_NONE.equals(param.defaultVal)) {
            return param.defaultVal;
        }
        if (param.require) {
            ValidUtil.notBlank(param.getName(), arg);
        }
        arg = ParamResolver.getParamValueByType(arg, param.type);
        return arg;
    }
}
